import apiClient from '@/api/interceptors';

/**
 * 获取项目列表
 */
export async function getProjectList(clusterId) {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/list`);
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '获取项目列表失败');
        }
    } catch (error) {
        console.error('请求项目列表失败:', error);
        throw error;
    }
}

/**
 * 获取项目分页数据
 */
export async function getProjectPage(clusterId, page, size) {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/page`, {
            params: { page, size }
        });
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '获取项目分页数据失败');
        }
    } catch (error) {
        console.error('请求项目分页数据失败:', error);
        throw error;
    }

}
// 工作流定义相关API

/**
 * 查询工作流定义分页
 */
export async function getProcessDefinitionPage(clusterId, projectCode, page, size, searchVal) {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/${projectCode}/process/page`, {
            params: { page, size, searchVal }
        });
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '查询工作流定义分页失败');
        }
    } catch (error) {
        console.error('请求工作流定义分页失败:', error);
        throw error;
    }
}

/**
 * 上线工作流定义
 */
export async function onlineProcessDefinition(clusterId, projectCode, processCode) {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/${projectCode}/process/${processCode}/online`);
        if (response.data.code === 1) {
            return true;
        } else {
            throw new Error(response.data.msg || '上线工作流定义失败');
        }
    } catch (error) {
        console.error('请求上线工作流定义失败:', error);
        throw error;
    }
}

/**
 * 下线工作流定义
 */
export async function offlineProcessDefinition(clusterId, projectCode, processCode) {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/${projectCode}/process/${processCode}/offline`);
        if (response.data.code === 1) {
            return true;
        } else {
            throw new Error(response.data.msg || '下线工作流定义失败');
        }
    } catch (error) {
        console.error('请求下线工作流定义失败:', error);
        throw error;
    }
}

/**
 * 删除工作流定义
 */
export async function deleteProcessDefinition(clusterId, projectCode, processCode) {
    try {
        const response = await apiClient.delete(`/dophin/${clusterId}/project/${projectCode}/process/${processCode}`);
        if (response.data.code === 1) {
            return true;
        } else {
            throw new Error(response.data.msg || '删除工作流定义失败');
        }
    } catch (error) {
        console.error('请求删除工作流定义失败:', error);
        throw error;
    }
}

// 工作流实例相关API

/**
 * 查询工作流实例分页
 */
export async function getProcessInstancePage(clusterId, projectCode, workflowCode, page, size) {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/${projectCode}/instance/page`, {
            params: { page, size, workflowCode }
        });
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '查询工作流实例分页失败');
        }
    } catch (error) {
        console.error('请求工作流实例分页失败:', error);
        throw error;
    }
}

/**
 * 重新运行工作流实例
 */
export async function reRunProcessInstance(clusterId, projectCode, processInstanceId) {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/${projectCode}/instance/${processInstanceId}/re_run`);
        if (response.data.code === 1) {
            return true;
        } else {
            throw new Error(response.data.msg || '重新运行工作流实例失败');
        }
    } catch (error) {
        console.error('请求重新运行工作流实例失败:', error);
        throw error;
    }
}

/**
 * 暂停工作流实例
 */
export async function pauseProcessInstance(clusterId, projectCode, processInstanceId) {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/${projectCode}/instance/${processInstanceId}/pause`);
        if (response.data.code === 1) {
            return true;
        } else {
            throw new Error(response.data.msg || '暂停工作流实例失败');
        }
    } catch (error) {
        console.error('请求暂停工作流实例失败:', error);
        throw error;
    }
}

/**
 * 停止工作��实例
 */
export async function stopProcessInstance(clusterId, projectCode, processInstanceId) {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/${projectCode}/instance/${processInstanceId}/stop`);
        if (response.data.code === 1) {
            return true;
        } else {
            throw new Error(response.data.msg || '停止工作流实例失败');
        }
    } catch (error) {
        console.error('请求停止工作流实例失败:', error);
        throw error;
    }
}

/**
 * 删除工作流实例
 */
export async function deleteProcessInstance(clusterId, projectCode, processInstanceId) {
    try {
        const response = await apiClient.delete(`/dophin/${clusterId}/project/${projectCode}/instance/${processInstanceId}`);
        if (response.data.code === 1) {
            return true;
        } else {
            throw new Error(response.data.msg || '删除工作流实例失败');
        }
    } catch (error) {
        console.error('请求删除工作流实例失败:', error);
        throw error;
    }
}


/**
 * 开始工作流实例
 */
export async function startProcessInstance(clusterId, projectCode, processInstanceCreateParam) {
    try {
        const response = await apiClient.post(`/dophin/${clusterId}/project/${projectCode}/instance/start`, processInstanceCreateParam);
        if (response.data.code === 1) {
            return true;
        } else {
            throw new Error(response.data.msg || '开始工作流实例失败');
        }
    } catch (error) {
        console.error('请求开始工作流实例失败:', error);
        throw error;
    }
}

// 任务实例相关API

/**
 * 查询任务实例分页
 */
export async function getTaskInstancePage(clusterId, projectCode, page, size, processInstanceId, processInstanceName = '') {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/${projectCode}/task/page`, {
            params: { page, size, processInstanceId, processInstanceName }
        });
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '查询任务实例分页失败');
        }
    } catch (error) {
        console.error('请求任务实例分页失败:', error);
        throw error;
    }
}

/**
 * 查询任务日志
 */
export async function getTaskLog(clusterId, projectCode, taskInstanceId, skipLineNum, limit) {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/${projectCode}/task/${taskInstanceId}/log`, {
            params: { skipLineNum, limit }
        });
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '查询任务日志失败');
        }
    } catch (error) {
        console.error('请求任务日志失败:', error);
        throw error;
    }
}

/**
 * 获取任务日志最后N行
 */
export async function getTaskLogTail(clusterId, projectCode, taskInstanceId, lastLineNum = 100) {
    try {
        const response = await apiClient.get(`/dophin/${clusterId}/project/${projectCode}/task/${taskInstanceId}/log/tail`, {
            params: { lastLineNum }
        });
        if (response.data.code === 1) {
            return response.data.data;
        } else {
            throw new Error(response.data.msg || '获取任务日志末尾内容失败');
        }
    } catch (error) {
        console.error('请求任务日志末尾内容失败:', error);
        throw error;
    }
}